/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vue;

import calcul.MonteCarloMulti;
import calcul.MonteCarloAbstract;
import calcul.MonteCarloSingle;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;

/**
 * Vue.
 * @author Jo
 */
public class Fenetre<T extends MonteCarloAbstract> extends JFrame implements Observer{
    
    MonteCarloAbstract calculator;
    
    JButton bouton;
    JLabel label;
    JProgressBar progressBar;
    
    /**
     * Init.
     * @param multithread 
     */
    public Fenetre(boolean multithread) {
        super();
        
        if(multithread) {
            this.calculator = new MonteCarloMulti();
        }
        else {
            this.calculator = new MonteCarloSingle();
        }
        this.calculator.addObserver(this);
        
        setSize(300, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Monte Carlo");
        setResizable(false);
        setLocationRelativeTo(null);
        initGUI();
        setVisible(true);
    }
    
    private void initGUI(){
        JMenuBar menuBar = new JMenuBar();
        JMenu test1 = new JMenu("Fichier");
        JMenu test1_2 = new JMenu("Sous ficher");
        JMenu test2 = new JMenu("Edition");
        JMenuItem item = new JMenuItem("I");
        
        test1.add(item);
        menuBar.add(test1);
        this.setJMenuBar(menuBar);
        
        JPanel panelCentre = new JPanel();
        JPanel panelBouton = new JPanel();
        
        
        bouton = new JButton("lancer!");
        bouton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //pb.setIndeterminate(true);
                bouton.setEnabled(false);
                label.setText("Calcul en cours");
                Thread thread_calculer = new Thread(calculator);
                thread_calculer.start();
            }
        });
        
        label = new JLabel("");
        
        int marge=5;
        progressBar = new JProgressBar();
        ((JComponent)this.getContentPane()).setBorder(BorderFactory.createEmptyBorder(marge, marge, marge, marge));
        
        panelBouton.setLayout(new BorderLayout());
        panelBouton.setBorder(BorderFactory.createEmptyBorder(marge, marge, marge, marge));
        panelBouton.add(bouton, BorderLayout.LINE_END);
        
        panelCentre.setLayout(new BorderLayout());
        panelCentre.add(label, BorderLayout.CENTER);
        panelCentre.add(panelBouton, BorderLayout.PAGE_END);
        
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(progressBar, BorderLayout.PAGE_END);
        this.getContentPane().add(panelCentre, BorderLayout.CENTER);
    }
    
    /**
     * Maj de la barre de progression ou affichage du resultat final.
     * @param o
     * @param arg 
     */
    @Override
    public void update(Observable o, Object arg) {
        ObjetEchange obj = (ObjetEchange)arg;
        float pourcent = obj.pourcent;
        double res = obj.res;
        boolean finished = obj.finished;
        
        if(finished){
            progressBar.setIndeterminate(false);
            bouton.setEnabled(true);
            
            label.setText(""+res);
        }
        else{
            int temp = (int) (progressBar.getMaximum()*pourcent);
            progressBar.setValue(temp);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) { }
        
        //true pour le multi-thread
        new Fenetre(false);
    }
    
}
